import 'package:flutter/material.dart';
import 'home_page.dart';
import 'file_upload_page.dart';
import 'profile_page.dart';
import 'function_page.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _currentIndex = 1; // 默认选中"文件"页
  bool _hideBottomNav = false;
  late final FileUploadPage _fileUploadPage;
  late final List<Widget> _pages;

  @override
  void initState() {
    super.initState();

    // 创建文件上传页面实例
    _fileUploadPage = FileUploadPage(
      onSelectModeChanged: (isSelectMode) {
        setState(() {
          _hideBottomNav = isSelectMode;
        });
      },
    );

    // 初始化页面列表
    _pages = [
      const HomePage(), // 首页
      _fileUploadPage, // 文件
      const FunctionPage(), // 功能
      const ProfilePage(), // 我的
    ];
  }

  void _onItemTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar:
          _hideBottomNav
              ? null
              : BottomNavigationBar(
                currentIndex: _currentIndex,
                onTap: _onItemTapped,
                selectedItemColor: Theme.of(context).primaryColor,
                unselectedItemColor: Colors.grey,
                type: BottomNavigationBarType.fixed, // 固定导航栏样式，超过3个项目时需要
                items: const [
                  BottomNavigationBarItem(
                    icon: Icon(Icons.home_outlined),
                    activeIcon: Icon(Icons.home),
                    label: '首页',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.folder_outlined),
                    activeIcon: Icon(Icons.folder),
                    label: '文件',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.apps_outlined),
                    activeIcon: Icon(Icons.apps),
                    label: '功能',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.person_outline),
                    activeIcon: Icon(Icons.person),
                    label: '我的',
                  ),
                ],
              ),
    );
  }
}
